package cn.youyeku.zsxh.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Properties;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.AppConfig;
import cn.youyeku.zsxh.Utils.CircleImageView;
import cn.youyeku.zsxh.Utils.ImageUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.Utils.Tools;
import cn.youyeku.zsxh.adapter.ViewPagerAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.app;
import cn.youyeku.zsxh.bean.User;
import cn.youyeku.zsxh.common.Contanst;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.HandleActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static cn.youyeku.zsxh.Utils.AppConfig.CONF_USER_BIRTHDAY;
import static cn.youyeku.zsxh.Utils.AppConfig.CONF_USER_SEX;
import static cn.youyeku.zsxh.Utils.AppConfig.CONF_USER_SIGN;


//朋友 个人中心主页
public class UserInfoActivity extends HandleActivity implements View.OnClickListener {


    @BindView(R.id.user_center_ic_back)
    Button userCenterIcBack;
    @BindView(R.id.btn_follows)
    Button btn_follows;
    @BindView(R.id.btn_private_message)
    Button btnCenterPrivateMessage;
    @BindView(R.id.btn_user_info)
    Button btnCenterUserInfo;
    @BindView(R.id.tv_personCentral_nickname)
    TextView tvPersonCentralNickname;
    @BindView(R.id.user_center_tv_follows)
    TextView userCenterTvFollows;
    @BindView(R.id.user_center_tv_fans)
    TextView userCenterTvFans;
    @BindView(R.id.user_center_tv_sign)
    TextView userCenterTvSign;
    @BindView(R.id.circleImageView)
    CircleImageView circleImageView;
    private FrameLayout frameLayout;
    private Boolean hasMeasured = false;
    private ImageView user_center_bg;
    private RelativeLayout user_center_relative;
    private int width;
    private String bundle_uid;


    private User user;

    @BindView(R.id.user_center_view_page)
    ViewPager mainViePage;
    @BindView(R.id.user_center_tabs)
    TabLayout mainTabs;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_user_center);
        ButterKnife.bind(this);
        Tools.initEffect(getWindow());

        if (savedInstanceState != null) {
// Restore value of members from saved state
            bundle_uid = savedInstanceState.getString("bundle_uid");
        } else {
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            bundle_uid = bundle.getString("bundle_uid");
        }
        initUserView();
        initTabsViewPage();

    }

    private void initUserView() {
        frameLayout = (FrameLayout) findViewById(R.id.user_frame);
        user_center_bg = (ImageView) findViewById(R.id.user_center_bg);
        user_center_relative = (RelativeLayout) findViewById(R.id.user_center_relative);

        //观察者 观察
        ViewTreeObserver vto = frameLayout.getViewTreeObserver();

        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!hasMeasured) {
                    LogUtils.w("=====");
                    int height = frameLayout.getMeasuredHeight();
                    width = frameLayout.getMeasuredWidth();

                    LogUtils.w(width + "   " + height);
                    //获取到宽度和高度后，可用于计算

                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) user_center_bg.getLayoutParams();
                    params.height = height / 2;
                    user_center_bg.setLayoutParams(params);
                    FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) user_center_relative.getLayoutParams();
                    params2.height = height / 2;
                    user_center_relative.setLayoutParams(params2);

                    hasMeasured = true;

                }
                return true;
            }
        });


        ImageUtils.GlideCircleImg(getApplicationContext(), circleImageView, XhApi.getFace(bundle_uid));

        if (bundle_uid.equals(AppConfig.CONF_USER_ID)) {
            btnCenterPrivateMessage.setVisibility(View.GONE);
            btn_follows.setVisibility(View.GONE);
        } else {
            btnCenterUserInfo.setVisibility(View.GONE);
        }


        XhApi.get_user_info(bundle_uid, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {


                try {
                    JSONObject object = new JSONObject(response.body().string());
                    int code = object.getInt("code");
                    Message msg = new Message();
                    if (code == 200) {
                        Gson gson = new Gson();
                        user = gson.fromJson(object.getString("data"), User.class);


                        CONF_USER_SEX = user.getSex();
                        CONF_USER_SIGN = user.getSign();
                        CONF_USER_BIRTHDAY = user.getBirthday();
                        Properties ps = AppConfig.getProps();
                        ps.setProperty("CONF_USER_SEX", CONF_USER_SEX);
                        ps.setProperty("CONF_USER_SIGN", CONF_USER_SIGN);
                        ps.setProperty("CONF_USER_BIRTHDAY", CONF_USER_BIRTHDAY==null?"没有设置":CONF_USER_BIRTHDAY);
                        AppConfig.set(ps);
                    }
                    msg.what = code;
                    handler.sendMessage(msg);


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    private void initTabsViewPage() {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new FgmUser(), "new");
        adapter.addFragment(new FgmUser(), "new");


        mainViePage.setAdapter(adapter);
        assert mainTabs != null;
        mainTabs.setupWithViewPager(mainViePage);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onChange() {

    }

    @Override
    protected void onError() {

    }

    @Override
    protected void onSuccess() {

        tvPersonCentralNickname.setText(user.getNick_name());
        userCenterTvSign.setText(user.getSign());
        userCenterTvFans.setText(user.getFans_count() + " 粉丝");
        userCenterTvFollows.setText(user.getFriends_count() + " 关注");


    }

    @OnClick({R.id.user_center_ic_back, R.id.btn_follows, R.id.btn_private_message, R.id.btn_user_info,R.id.user_center_tv_follows, R.id.user_center_tv_fans})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_center_ic_back:
                finish();
                break;
            case R.id.btn_follows:
                XhApi.add_friends(bundle_uid, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        try {
                            JSONObject object = new JSONObject(response.body().string());
                            app.backgroundThreadShortToast(object.getString("message"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                });

                break;
            case R.id.btn_private_message:
                UIControl.showPrivateMessage(getApplicationContext(),bundle_uid,user.getNick_name());
                break;
            case R.id.btn_user_info:
                UIControl.showUserProfile(getApplicationContext());
                break;
            case R.id.user_center_tv_follows:
                UIControl.showFansFollowActivity(getApplicationContext(), Contanst.USER_CETER_FOLLOWS);
                break;
            case R.id.user_center_tv_fans:
               UIControl.showFansFollowActivity(getApplicationContext(),Contanst.USER_CETER_FANS);
                break;
        }
    }


}
